package com.iMusic.presentationLayer;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.iMusic.businessLogicLayer.Account;
import com.iMusic.businessLogicLayer.Cart;
import com.iMusic.businessLogicLayer.Order;
import com.iMusic.businessLogicLayer.Payment;
import com.iMusic.businessLogicLayer.Product;
import com.iMusic.businessLogicLayer.orderStatus.OrderStatus;
import com.iMusic.integrationLayer.BankInterface;
import com.iMusic.util.UserMessages;

public class Checkout extends HttpServlet {

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		HttpSession session = request.getSession();
		Account account = (Account) session.getAttribute("Account");
		String redirection = "checkoutFS.jsp";
		Cart cart = null;
		
		session.removeAttribute("Error");
		
		if (account != null) {
			cart = account.getCart();
			
			if (cart == null) {
				session.setAttribute("Error", UserMessages.EMPTY_CART);
				redirection += "?error="+UserMessages.EMPTY_CART;
			}

			Order order = null;
			try {
				order = cart.generateOrder();
			} catch (SQLException e2) {
				e2.printStackTrace();
			}
			
			String cardNumber = (String) request.getParameter("cardNumber");
			String paymentMethod = (String) request.getParameter("paymentMethod");
			String expiryDate = (String) request.getParameter("expiryDate");
			String validationNumber = (String) request.getParameter("validationNumber");
			String nameOnCard = (String) request.getParameter("nameOnCard");
			String cardFlag = (String) request.getParameter("cardFlag");
			
			Payment payment = new Payment(cardNumber, paymentMethod, expiryDate, validationNumber, nameOnCard, order.getTotal(), cardFlag);
			
			order.setPayment(payment);
			
			boolean paymentAuthorized = false;
			boolean commError = false;
			
			try {
				paymentAuthorized = BankInterface.authorizePayment(payment);
			}
			catch (Exception e) {
				System.err.println("Error in communication with Banks. " + e.getMessage());
				paymentAuthorized = false;
				commError = true;
			}
			
			if (paymentAuthorized) {
				order.setPaid(true);
				order.generateReceipt();
				order.setStatus(OrderStatus.InProgress);
				//order.sendEmail();
				
				try {
					List<Product> prodList = order.getDownloadables();
					session.setAttribute("ProdList", prodList);
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
				
				try {
					order.storeOrder();
				} catch (SQLException e) {
					e.printStackTrace();
				}
				System.out.println("Payment of $" + order.getTotal() + " authorized. Receipt number: " + order.getReceiptNumber());
			}
			else {
				if (commError) {
					session.setAttribute("Error", UserMessages.BANK_COMM_ERROR);
					redirection += "?error="+UserMessages.BANK_COMM_ERROR;
				}
				else {
					System.out.println("Payment not authorized.");									
					session.setAttribute("Error", UserMessages.PAYMENT_BOT_AUTHORIZED);
					redirection += "?error="+UserMessages.PAYMENT_BOT_AUTHORIZED;
				}
			}
			
			session.setAttribute("Order", order);
		}
		else {
			session.setAttribute("Error", UserMessages.USER_NOT_LOGGEDIN);
			redirection += "?error="+UserMessages.USER_NOT_LOGGEDIN;
		}
		

		response.sendRedirect(redirection);
	}
	
}

